﻿using MinecraftClient;
using MinecraftClient.Mapping;
using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Linq;
using System.Text.RegularExpressions;
using MinecraftClient.Commands;

namespace MinecraftClient.ChatBots.MyBots
{
    public class CraftHelper : ChatBot
    {
        public override void Initialize()
        {
            if (!BotIsRunning())
            {
                SendText("AutoCraft is not running. Automatically unloading CraftHelper.");
                UnloadBot();
            }

            SendText("Script info will be logged to the console.");
        }

        public override void GetText(string text, string json)
        {
            text = GetVerbatim(text);
            Regex load = new Regex(@"^\[.+\]<Gare_TH> ac load$");
            Regex lst = new Regex(@"^\[.+\]<Gare_TH> ac lst$");
            Regex reload = new Regex(@"^\[.+\]<Gare_TH> ac reload$");
            Regex resetcfg = new Regex(@"^\[.+\]<Gare_TH> ac resetcfg$");
            Regex start = new Regex(@"^\[.+\]<Gare_TH> ac start (.+)$");
            Regex stop = new Regex(@"^\[.+\]<Gare_TH> ac stop$");

            if (load.IsMatch(text))
            {
                CmdLoad();
                return;
            }

            if (lst.IsMatch(text))
            {
                CmdList();
                return;
            }

            if (reload.IsMatch(text))
            {
                CmdReload();
                return;
            }

            if (resetcfg.IsMatch(text))
            {
                CmdResetcfg();
                return;
            }

            if (start.IsMatch(text))
            {
                string recipe = start.Match(text).Groups[1].Value;
                CmdStart(recipe);
                return;
            }

            if (stop.IsMatch(text))
            {
                CmdStop();
            }
        }

        private void CmdStop()
        {
            SendText("AutoCraft stopped.");
            PerformInternalCommand("ac stop");
        }

        private void CmdStart(string recipe)
        {
            SendText("Please check console.");
            PerformInternalCommand("ac start " + recipe);
        }

        private void CmdResetcfg()
        {
            SendText("Clear the config file.");
            PerformInternalCommand("ac resetcfg");
        }

        private void CmdReload()
        {
            SendText("Current crafting recipes is logged to the console.");
            PerformInternalCommand("ac reload");
        }

        private void CmdList()
        {
            SendText("Current crafting recipes is logged to the console.");
            PerformInternalCommand("ac list");
        }

        private void CmdLoad()
        {
            SendText("Config file loaded.");
            PerformInternalCommand("ac load");
        }

        private bool BotIsRunning()
        {
            List<ChatBot> bots = GetLoadedChatBots();
            foreach (var b in bots)
            {
                if (b.GetType().Name.Equals("AutoCraft")) return true;
            }
            return false;
        }
        
        
    }
}